package com.hzqy.commons.utils;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

public class TestUtils {
	public static String getWeiXinJson(String url_) {
		String Result=null;
		InputStream is = null;
		try{
			URL url = new URL(url_);
			HttpURLConnection httpUrlConnection=(HttpURLConnection) url.openConnection();
			httpUrlConnection.setDoOutput(true);
			httpUrlConnection.setDoInput(true);
			httpUrlConnection.setUseCaches(false);
			httpUrlConnection.setRequestProperty("Content-type", "application/x-java-serialized-object");
			httpUrlConnection.setRequestMethod("POST");
			httpUrlConnection.connect();
			is = httpUrlConnection.getInputStream();
			int resp = 0;
			ByteArrayOutputStream bos = new ByteArrayOutputStream();
			while((resp = is.read())!=-1) {
				bos.write(resp);
			}
			Result= new String(bos.toByteArray(),"UTF-8");
		}catch(Exception e){
			e.printStackTrace();
		}finally {
			if (is != null) {
				try {
					is.close();
				} catch (IOException e) {
				}
			}
		}
		System.out.println(Result);
		return Result;
	}

	public static void main(String args[]) throws Exception {
		String appid="wx6d91b1423743ee77";
		String secret="e9b0ca2f6dc8148ed21212255776159f";
		String code="";
		String openid="oslMvv3GTG5MapOx7N0fKgcO-ULc";
		//String weixinUrl= "https://api.weixin.qq.com/sns/oauth2/access_token?appid="+appid+"&secret="+secret+"&code="+code+"&grant_type=authorization_code";
		String weixinUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+appid+"&secret="+secret;
		String Result=getWeiXinJson(weixinUrl);
		JSONObject obj = JSONObject.parseObject(Result);
		//String openid=(String)obj.get("openid");
		String access_token=(String)obj.get("access_token");
		//System.out.println(access_token);

//		String weixinUrl2= "https://api.weixin.qq.com/sns/userinfo?access_token="+access_token+"&openid="+openid+"&lang=zh_CN";
//		String weixinUrl2 = "https://api.weixin.qq.com/cgi-bin/user/info?access_token="+access_token+"&openid="+openid;
//		Result=getWeiXinJson(weixinUrl2);
//		JSONObject jsonObject = JSONObject.parseObject(Result);
//		String nickname=(String)jsonObject.get("nickname");

	}
}
